<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/icons.css" />
    <script src="../script/jquery-1.9.1.min.js"></script>
    <script src="../script/bootstrap.min.js"></script>
    <style>
        .table tr:first-child {
            background: url(static/img/tableheader-bg.gif) no-repeat left top;
            background-color: #DCE9F2;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 13px;
            font-weight: bold;
        }

        .table {
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
        }

        .table td {
            height: 40px;
            text-align: center;
            border: 1px solid #CBD6DE;
            padding: 0;
            margin: 0;
            vertical-align: middle !important;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            开班点检
        </div>
    </header>
    <div class="aui-content" style="margin-top:50px;">

        <div class="aui-form">

            <div class="aui-content aui-margin-b-15">
                <ul class="aui-list aui-form-list" style="font-size: 14px;">
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label" style="font-size: 14px;">设备编码</div>
                            <input type="text" class="aui-input" placeholder="请 扫描/输入 设备编码"
                                style="width: 80%;font-size: 14px;" id="sm">
                            <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem"
                                onclick="toScanner();"></i>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label" style="font-size: 14px;">点检任务</div>
                            <input type="text" class="aui-input" style="width: 80%;font-size: 14px;" id="chkTaskCode"
                                value="" disabled>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label" style="font-size: 14px;">执行状态</div>
                            <div class="aui-list-item-input">
                                <select id="zxState" style="font-size: 14px;">
                                    <option value="2" selected="true">进行</option>
                                    <option value="3">完成</option>
                                </select>
                            </div>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label" style="font-size: 14px;">执行结果</div>
                            <div class="aui-list-item-input">
                                <select id="zxResult" style="font-size: 14px;" disabled>
                                    <option value="0">未判定</option>
                                    <option value="1">正常</option>
                                    <option value="2">异常</option>
                                </select>
                            </div>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label" style="width:30%;font-size: 14px;">实际开始时间</div>
                            <div class="aui-list-item-input" style="float:left;width: 70%;">
                                <input type="text" id="actualBeginDate" onclick="showTime();">
                            </div>
                        </div>
                    </li>
                    <li class="aui-list-item" style="display: none;">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label" style="width:20%;font-size: 14px;">实际结束时间</div>
                            <div class="aui-list-item-input" style="float:left;width: 75%;">
                                <input type="text" id="actualEndDate" onclick="showTime2();">
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <ul id="myTab" class="nav nav-tabs" style="background-color: #F4F4F4;font-size: 13px;">
                <li class="active" style="width: 100%;text-align:center;"> <a href="#item" data-toggle="tab">检修项 </a>

                </li>

            </ul>
            <div id="myTabContent" class="tab-content">
                <div class="tab-pane fade in active" id="item">
                    <div style="background-color:white; height: 100%;width: 100%;">
                        <table id="chkItemTable" class="table" style="width:200%;">
                            <thead id="chkItemTableHead">
                            </thead>
                            <tbody id="chkItemTableBody">
                            </tbody>
                            <tbody id="chkItemTableBehind">
                            </tbody>
                        </table>
                        <div class="aui-content">
                            <div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
                                <p>
                                </p>
                                <div class="aui-btn aui-btn-info" onclick="doAdd()"
                                    style="width: 100%;height: 100%;display: none;">
                                    <span class="aui-iconfont" style="font-size: 20px;">添加检修项</span>

                                </div>
                                <p></p>
                            </div>
                            <div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
                                <p>
                                </p>
                                <div class="aui-btn aui-btn-info" onclick="doDel()"
                                    style="width: 100%;height: 100%;display: none;">
                                    <span class="aui-iconfont" style="font-size: 20px;">删除检修项</span>

                                </div>
                                <p></p>
                            </div>
                            <div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
                                <p>
                                </p>
                                <div class="aui-btn aui-btn-info" onclick="doSave()" style="width: 100%;height: 100%;">
                                    <span class="aui-iconfont" style="font-size: 20px;">提交数据</span>

                                </div>
                                <p></p>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script>

    var taskId = "";
    var taskCode = "";
    var itemIds = "";
    var itemCodes = "";
    var itemNames = "";
    var itemModes = "";
    var itemValues = "";
    var itemMaxValues = "";
    var itemMinValues = "";
    var itemStandardValue = "";
    var josnList = [];
    var delItemIds = "";
    var str = "";

    var equipUniqueId = "";   //设备唯一号
    var workCenterCode = "";  //生产线编码
    var opCode = "";          //工序编码
    var shiftCode = "";       //班次编码
    var shiftName = "";       //班次名称
    var proddate = "";        //生产日期
    var shiftId = "";
    var workCellCode = "";    //工位编码



    apiready = function () {
        $("#sm").focus();
        idSelector = "sm";
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //1:查询产线工序信息
        searchWorkCenterData();


    }
    //查询产线工位信息
    function searchWorkCenterData() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getOpCodeByUniCode";
        var data = {
            values: {
                clientCode: equipUniqueId,
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    workCenterCode = myobj.workCenterCode;
                    workCellCode = myobj.workCellCode;
                    opCode = myobj.opCode;
                    if (opCode != null && opCode != "") {

                        //1:查询当前操作人员信息
                        searchDutyPersonData();


                    }

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询人员信息
    function searchDutyPersonData() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getProductionAssemblyTake";
        var data = {
            values: {
                workCenterCode: workCenterCode,
                opCode: opCode,
                workCellCode: workCellCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    shiftCode = myobj.shiftCode;
                    shiftName = myobj.shiftName;
                    proddate = myobj.proddate;
                    shiftId = myobj.shiftId;

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });

                }
            } else {
                alert("网络连接超时!")
            }
        });
    }
    //7；PDA自带扫描回车事件-设备编码
    $("#sm").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //设备编码
            $("#sm").blur();
            $("#sm").val(sm);
            getDailyTask(sm)
        }
    });

    function getDailyTask(sm) {
        console.log(11111111)
        if (opCode == null || opCode == "" || opCode == undefined) {
            api.toast({
                msg: "唯一号【" + equipUniqueId + "】未配置工序信息,不允许操作",
                duration: 3000
            });
            return;
        }
        var query = api.pageParam.query;
        var type = "";
        if (query.type != undefined && query.type != null && query.type != "") {
            type = query.type;
        }

        api.showProgress({
            title: '查询中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getDailyTask";
        var data = {
            values: {
                equipCode: sm,//设备唯一号
                un: un,//用户
                shiftId: shiftId,
                shiftCode: shiftCode,//班次
                proddate: proddate,//日期
                workCellCode: workCellCode,//工位
                type: type
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == 0) {
                    document.getElementById("chkTaskCode").value = myobj.chkTaskCode
                    var id = myobj.taskId
                    var code = myobj.taskCode
                    var equipId = myobj.equipId
                    var equipCode = myobj.SRequipCode
                    var actualBeginDate = myobj.actualBeginDate
                    var actualEndDate = myobj.actualEndDate
                    var actualEndDate = myobj.actualEndDate
                    var taskResult = myobj.taskResult
                    var taskState = myobj.taskState
                    setEquipTask(id, code, equipId, equipCode, actualBeginDate, actualEndDate, taskResult, taskState)

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });

                }
            } else {
                api.hideProgress();
                alert("网络连接超时!")
            }
        });
    }

    // //查询人员信息
    // function searchDutyPersonData() {
    //     var un = localStorage.getItem('un');
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/getProductionAssemblyTake";
    //     var data = {
    //         values: {
    //             workCenterCode: workCenterCode,
    //             opCode: opCode,
    //             un: un,
    //         }
    //     };
    //     var params = new Object();
    //     params.url = url;
    //     params.dataType = 'json';
    //     params.method = 'post';

    //     ajaxForData(params, data, false, function (ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var myobj = eval("(" + data + ")");

    //             if (myobj.code == 0) {

    //                 shiftCode = myobj.shiftCode;
    //                 shiftName = myobj.shiftName;
    //                 proddate = myobj.proddate;
    //                 shiftId = myobj.shiftId;

    //             } else {
    //                 api.toast({
    //                     msg: myobj.msg,
    //                     duration: 3000
    //                 });

    //             }
    //         } else {
    //             alert("网络连接超时!")
    //         }
    //     });
    // }
    function showTime() {
        var moduleTimer = api.require('moduleTimer');
        moduleTimer.showTimer({
            startTime: '2000-01-1 00:00',//可选择的起始时间
            endTime: '2099-12-31 23:59',//可选择的结束时间
            timeFormat: 'yyyy-MM-dd HH:mm:ss',//返回的时间格式，如果不传默认是yyyy-MM-dd HH:mm:ss
            selColor: '#079962'//选中的颜色值
        }, function (ret, err) {
            var actualBeginDate = ret;
            var text = document.getElementById("actualBeginDate");
            text.value = actualBeginDate;
        });
    }

    function showTime2() {
        var moduleTimer = api.require('moduleTimer');
        moduleTimer.showTimer({
            startTime: '2000-01-1 00:00',//可选择的起始时间
            endTime: '2099-12-31 23:59',//可选择的结束时间
            timeFormat: 'yyyy-MM-dd HH:mm:ss',//返回的时间格式，如果不传默认是yyyy-MM-dd HH:mm:ss
            selColor: '#079962'//选中的颜色值
        }, function (ret, err) {
            var actualEndDate = ret;
            var text = document.getElementById("actualEndDate");
            text.value = actualEndDate;
        });
    }

    function toScanner() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150,//扫描窗口的上边距
            width: 250,//扫描窗口的宽
            height: 250,//扫描窗口的高
            useColor: false,//是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF',//渐变起始颜色
            endColor: '#7F08C8FF',//渐变结束颜色
            animTime: 2200,//扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //console.log(JSON.stringify(ret));
            $("#sm").blur();
            var SMequipCode = ret;
            var text = document.getElementById("sm");
            text.value = SMequipCode;
            getDailyTask(sm)

        });

    }

    function goSelectChkTask() {
        var SRequipCode = document.getElementById("sm").value;
        if (SRequipCode == '') {
            //如果为空
            alert("请先扫设备编号");
            return;
        }
        var query = api.pageParam.query;
        var type = "";
        if (query.type != undefined && query.type != null && query.type != "") {
            type = query.type;
        }
        var url = "selEquipTaskList.html";
        var title = "点检任务列表";
        api.openWin({
            name: 'selEquipTaskList',
            url: 'selEquipTaskList.html',
            pageParam: {
                url: url,
                title: title,
                SRequipCode: SRequipCode,
                type: type
            }
        });
    }

    function setEquipTask(id, code, equipId, equipCode, actualBeginDate, actualEndDate, taskResult, taskState) {
        taskId = id;
        equipId = equipId;
        $("#zxResult").children().remove();
        if (taskResult == 0) {
            var str = "<option value='0' selected='true'>未判定</option><option value='1' >正常</option><option value='2'>异常</option>";
            $("#zxResult").append(str);
        } else if (taskResult == 1) {
            var str = "<option value='0'>未判定</option><option value='1' selected='true'>正常</option><option value='2'>异常</option>";
            $("#zxResult").append(str);
        } else {
            var str = "<option value='0'>未判定</option><option value='1' >正常</option><option value='2' selected='true'>异常</option>";
            $("#zxResult").append(str);
        }

        $("#zxState").children().remove();
        if (taskState == 2) {
            var str = "<option value='2' selected='true'>进行</option><option value='3' >完成</option>";
            $("#zxState").append(str);
        } else if (taskState == 3) {
            var str = "<option value='2'>进行</option><option value='3' selected='true'>完成</option>";
            $("#zxState").append(str);
        } else {
            var str = "<option value='2' selected='true'>进行</option><option value='3' >完成</option>";
            $("#zxState").append(str);
        }
        console.log(22222222)

        $("#chkTaskCode").attr("value", code);
        $("#actualBeginDate").attr("value", actualBeginDate);
        $("#actualEndDate").attr("value", actualEndDate);
        if ("" != taskId) {
            //选择设备点检任务后，根据任务id获取点检项
            getEquipTaskItemByTask(taskId, equipId);
        }

    }


    function getEquipTaskItemByTask(taskId, equipId) {
        console.log(333333)
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getChkItemByTask";
        var un = localStorage.getItem('un');

        var params = new Object();
        params.url = url;
        params.method = 'post';
        params.dataType = 'json';
        params.returnAll = 'false';
        params.timeout = 60;

        var data = {
            values: {
                un: un,
                taskId: taskId,
                equipId: equipId
            }
        };


        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    console.log(444444444)
                    itemIds = "";
                    itemCodes = "";
                    itemNames = "";
                    itemModes = "";
                    itemValues = "";
                    itemMaxValues = "";
                    itemMinValues = "";
                    itemStandardValue = "";

                    josnList = [];
                    var list = myobj.list;
                    var html = "";
                    var html1 = "";
                    html1 += "<tr id='tr1' color: #467aa7;>";
                    html1 += "<td width='10%' algin='center'>序号</td>";
                    html1 += "<td algin='center' style='display:none;'>编号</td>";
                    html1 += "<td width='25%' algin='center'>点检项</td>";
                    html1 += "<td width='15%' algin='center'>最大值</td>";
                    html1 += "<td width='15%' algin='center'>标准值</td>";
                    html1 += "<td width='15%' algin='center'>最小值</td>";
                    html1 += "<td width='25%' algin='center'>检测值</td></tr>";

                    $api.html($api.byId("chkItemTableHead"), html1);
                    str = "";
                    console.log(JSON.stringify(list))
                    if (list != undefined) {
                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            var itemId = list[i].itemId;
                            var itemCode = list[i].itemCode;
                            var itemName = list[i].itemName;
                            var chkMode = list[i].chkMode;
                            var maxValue = list[i].maxValue;
                            var chkValue = list[i].chkValue;
                            var minValue = list[i].minValue;
                            var standardValue = list[i].standardValue;
                            var chkResult = list[i].chkResult;

                            if (i == 0) {
                                itemIds = itemId + ":" + chkMode;
                                itemCodes = itemCode;
                                itemNames = itemName;
                                itemMaxValues = maxValue;
                                itemMinValues = minValue;
                                itemStandardValue = standardValue;

                            } else {
                                itemIds = itemIds + "," + itemId + ":" + chkMode;
                                itemCodes = itemCodes + "," + itemCode;
                                itemNames = itemNames + "," + itemName;
                                itemMaxValues = itemMaxValues + "," + maxValue;
                                itemMinValues = itemMinValues + "," + minValue;
                                itemStandardValue = itemStandardValue + "," + standardValue;
                            }

                            if (chkMode == "0") {

                                if (chkResult == "1") {
                                    str += "<tr ><td width='10%' algin='center'>" +
                                        "" + "<input type='checkbox' onclick='setDelItem(\"" + itemId + "\");'></td><td algin='center' style='display:none;'>"
                                        + itemCode + "</td><td width='25%' algin='center'>"
                                        + itemName + "</td><td width='15%' algin='center'>"
                                        + maxValue + "</td><td width='15%' algin='center'>"
                                        + standardValue + "</td><td width='15%' algin='center'>"
                                        + minValue + "</td><td width='25%' algin='left'><select id='"
                                        + itemId + "'><option value='0'>未判定</option><option value='1' selected='true'>正常</option><option value='2'>异常</option></select></td></tr>"
                                } else if (chkResult == "2") {
                                    str += "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\"" + itemId + "\");'></td><td algin='center' style='display:none;'>"
                                        + itemCode + "</td><td width='25%' algin='center'>"
                                        + itemName + "</td><td width='15%' algin='center'>"
                                        + maxValue + "</td><td width='15%' algin='center'>"
                                        + standardValue + "</td><td width='15%' algin='center'>"
                                        + minValue + "</td><td width='25%' algin='left'><select id='"
                                        + itemId + "'><option value='0'>未判定</option><option value='1'>正常</option><option value='2' selected='true'>异常</option></select></td></tr>"
                                } else {
                                    str += "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
                                        + itemId + "\");'></td><td algin='center' style='display:none;'>"
                                        + itemCode + "</td><td width='25%' algin='center'>"
                                        + itemName + "</td><td width='15%' algin='center'>"
                                        + maxValue + "</td><td width='15%' algin='center'>"
                                        + standardValue + "</td><td width='15%' algin='center'>"
                                        + minValue + "</td><td width='25%' algin='left'><select id='"
                                        + itemId + "'><option value='0' selected='true'>未判定</option><option value='1'>正常</option><option value='2'>异常</option></select></td></tr>"
                                }

                            }
                            // if (chkMode == "1") {
                            //     str += "<tr ><td width='10%' algin='center'>"
                            //         + "" + "<input type='checkbox' onclick='setDelItem(\"" + itemId + "\");'></td><td algin='center' style='display:none;'>"
                            //         + itemCode + "</td><td width='50%' algin='center'>" + itemName + "</td><td width='15%' algin='center'>"
                            //         + standardValue + "</td><td width='25%' algin='center'><input id='" + itemId
                            //         + "' type='text' value='" + chkValue + "'></td></tr>"
                            // }
                            if (chkMode == "1") {
                                str += "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
                                    + itemId + "\");'></td><td algin='center' style='display:none;'>"
                                    + itemCode + "</td><td width='25%' algin='center'>"
                                    + itemName + "</td><td width='15%' algin='center'>"
                                    + maxValue + "</td><td width='15%' algin='center'>"
                                    + standardValue + "</td><td width='15%' algin='center'>"
                                    + minValue + "</td><td width='25%' algin='left'><input id='"
                                    + itemId + "' type='text' value='"
                                    + chkValue + "'></td></tr>"
                            }
                            console.log(itemMaxValues);
                            console.log(itemMinValues);
                        }
                    }
                    $("#chkItemTableHead").append(str);

                } else if (myobj.code == '1') {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            }
        });
    }

    // function setAddItem(itemStrs) {
    //     if (itemStrs == "") {
    //         //未添加点检项
    //     } else {
    //         //将选择添加的点检项添加到表格中
    //         var itemStrArr = itemStrs.split(",");
    //         // alert(itemStrArr);
    //         var trStr = "";
    //         var nums = itemIds.split(",").length;
    //         var nums2 = itemIds.split(",");
    //         for (i = 0; i < itemStrArr.length; i++) {
    //             var itemArr = itemStrArr[i].split(":");
    //             console.log(itemArr);
    //             var id = itemArr[0];
    //             var code = itemArr[1];
    //             var name = itemArr[2];
    //             var mode = itemArr[3];
    //             var maxValue = itemArr[4];
    //             var minValue = itemArr[5];
    //             var standardValue = itemArr[6];

    //             if (itemIds == "") {
    //                 itemIds = id + ":" + mode;
    //                 itemCodes = code;
    //                 itemNames = name;
    //             } else {
    //                 itemIds = itemIds + "," + id + ":" + mode;
    //                 itemCodes = itemCodes + "," + code;
    //                 itemNames = itemNames + "," + name;
    //             }
    //             if (nums == 1) {
    //                 if (nums2 == "") {
    //                     if (mode == "0") {
    //                         trStr = "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
    //                             + id + "\");'></td><td algin='center' style='display:none;'>"
    //                             + code + "</td><td width='25%' algin='center'>"
    //                             + name + "</td><td width='15%' algin='center'>"
    //                             + maxValue + "</td><td width='15%' algin='center'>"
    //                             + standardValue + "</td><td width='15%' algin='center'>"
    //                             + minValue + "</td><td width='30%' algin='left'><select id='"
    //                             + id + "'><option value='0' selected='true'>未判定</option><option value='1'>正常</option><option value='2'>异常</option></select></td></tr>"
    //                     }
    //                     if (mode == "1") {
    //                         trStr = "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
    //                             + id + "\");'></td><td algin='center' style='display:none;'>"
    //                             + code + "</td><td width='25%' algin='center'>"
    //                             + name + "</td><td width='15%' algin='center'>"
    //                             + maxValue + "</td><td width='15%' algin='center'>"
    //                             + standardValue + "</td><td width='15%' algin='center'>"
    //                             + minValue + "</td><td width='30%' algin='center'><input id='"
    //                             + id + "' type='text' value=''></td></tr>"
    //                     }
    //                 } else {
    //                     if (mode == "0") {
    //                         trStr = "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
    //                             + id + "\");'></td><<td algin='center' style='display:none;'>"
    //                             + code + "</td><td width='25%' algin='center'>"
    //                             + name + "</td><td width='15%' algin='center'>"
    //                             + maxValue + "</td><td width='15%' algin='center'>"
    //                             + standardValue + "</td><td width='15%' algin='center'>"
    //                             + minValue + "</td><td width='30%' algin='left'><select id='"
    //                             + id + "'><option value='0' selected='true'>未判定</option><option value='1'>正常</option><option value='2'>异常</option></select></td></tr>"
    //                     }
    //                     if (mode == "1") {
    //                         trStr = "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
    //                             + id + "\");'></td><td algin='center' style='display:none;'>"
    //                             + code + "</td><td width='25%' algin='center'>"
    //                             + name + "</td><td width='15%' algin='center'>"
    //                             + maxValue + "</td><td width='15%' algin='center'>"
    //                             + standardValue + "</td><td width='15%' algin='center'>"
    //                             + minValue + "</td><td width='30%' algin='center'><input id='"
    //                             + id + "' type='text' value=''></td></tr>"
    //                     }
    //                 }
    //             } else {
    //                 if (mode == "0") {
    //                     trStr = "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
    //                         + id + "\");'></td><td algin='center' style='display:none;'>"
    //                         + code + "</td><<td width='25%' algin='center'>"
    //                         + name + "</td><td width='15%' algin='center'>"
    //                         + maxValue + "</td><td width='15%' algin='center'>"
    //                         + standardValue + "</td><td width='15%' algin='center'>"
    //                         + minValue + "</td><td width='30%' algin='left'><select id='"
    //                         + id + "'><option value='0' selected='true'>未判定</option><option value='1'>正常</option><option value='2'>异常</option></select></td></tr>"
    //                 }
    //                 if (mode == "1") {
    //                     trStr = "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
    //                         + id + "\");'></td><td algin='center' style='display:none;'>"
    //                         + code + "</td><td width='25%' algin='center'>"
    //                         + name + "</td><td width='15%' algin='center'>"
    //                         + maxValue + "</td><td width='15%' algin='center'>"
    //                         + standardValue + "</td><td width='15%' algin='center'>"
    //                         + minValue + "</td><td width='30%' algin='center'><input id='"
    //                         + id + "' type='text' value=''></td></tr>"
    //                 }
    //             }

    //             $("#chkItemTableHead").append(trStr);
    //         }
    //         if (trStr != "") {

    //             //	$("#chkItemTableBody").append(trStr);
    //         }
    //     }
    // }

    function doAdd() {
        var taskCode = $("#chkTaskCode").val();
        //	alert(itemCodes);
        if (taskCode != "") {
            //根据现有的点检项，获取新的点检项		
            var url = "selEquipItemList.html";
            var title = "设备检测项列表";
            api.openWin({
                name: 'equipItem',
                url: url,
                pageParam: {
                    title: title,
                    itemCodes: itemCodes
                }
            });
        } else {
            alert("请选择点检任务");
        }
    }




    function setDelItem(id) {
        if (delItemIds == "") {
            delItemIds = id;
        } else {
            if (delItemIds.indexOf(id) > 0) {
                //已存在
                var delArr = delItemIds.split(",");
                delItemIds = "";
                for (i = 0; i < delArr.length; i++) {
                    if (delArr[i] != id) {
                        if (i == 0) {
                            delItemIds = delArr[i];
                        } else {
                            delItemIds = delItemIds + "," + delArr[i];
                        }
                    }

                }
            } else {
                //不存在
                delItemIds = delItemIds + "," + id;
            }
        }
    }




    function doDel() {
        if (delItemIds != "") {
            //过滤掉要删除的点检项
            setItemValues();


            var idArr = itemIds.split(",");
            var codeArr = itemCodes.split(",");
            var nameArr = itemNames.split(",");
            var maxValueArr = itemMaxValues.split(",");
            var minValueArr = itemMinValues.split(",");
            var standardValueArr = itemStandardValue.split(",");
            var valueArr = itemValues.split(",");

            itemIds = "";
            itemCodes = "";
            itemNames = "";
            maxValues = "";
            minValues = "";
            standardValues = "";
            josnList = [];

            var itemIdStr = "";

            //$("#chkItemTableHead").children().remove();
            $("#chkItemTableHead").children().remove();
            $("#chkItemTableHead").append("<tr id='tr1'><td width='10%' algin='center'>序号</td><td width='10%' algin='center'>编号</td><td width='25%' algin='center'>点检项</td><td width='15%' algin='center'>最大值</td><td width='15%' algin='center'>标准值</td><td width='15%' algin='center'>最小值</td><td width='30%' algin='center'>检测值</td></tr>");
            //var html = "<tr id='tr1'><td width='10%' algin='center'>序号</td><td width='10%' algin='center'>编号</td><td width='50%' algin='center'>点检项</td><td width='30%' algin='center'></td></tr>";
            //$api.html($api.byId("chkItemTableHead"), html);
            var seq = 0;
            for (i = 0; i < idArr.length; i++) {
                itemIdStr = idArr[i].split(":")[0];
                if (delItemIds.indexOf(itemIdStr) < 0) {
                    seq++;
                    //不在要删除的id字符串中
                    var strDel = "";
                    if (i == 0) {
                        itemIds = idArr[i];
                        itemCodes = codeArr[i];
                        itemNames = nameArr[i];
                        maxValues = maxValueArr[i];
                        minValues = minValueArr[i];
                        standardValues = standardValueArr[i];
                    } else {
                        itemIds = itemIds + "," + idArr[i];
                        itemCodes = itemCodes + "," + codeArr[i];
                        itemNames = itemNames + "," + nameArr[i];
                        maxValues = maxValues + "," + maxValueArr[i];
                        minValues = minValues + "," + minValueArr[i];
                        standardValues = standardValues + "," + standardValueArr[i];
                    }
                    if (idArr[i].split(":")[1] == "0") {
                        if (valueArr[i] == "1") {
                            strDel = "<tr ><td width='10%' algin='center'><input type='checkbox' onclick='setDelItem(\""
                                + itemIdStr + "\");'></td><td width='10%' algin='center style='display:none; '>"
                                + codeArr[i] + "</td><td width='25%' algin='center'>"
                                + nameArr[i] + "</td><td width='15%' algin='center'>"
                                + maxValueArr[i] + "</td><td width='15%' algin='center'>"
                                + minValueArr[i] + "</td><td width='15%' algin='center'>"
                                + standardValueArr[i] + "</td><td width='30%' algin='left'><select id='"
                                + itemIdStr + "'><option value='0'>未判定</option><option value='1' selected='true'>正常</option><option value='2'>异常</option></select></td></tr>"
                        } else if (valueArr[i] == "2") {
                            strDel = "<tr ><td width='10%' algin='center'><input type='checkbox' onclick='setDelItem(\""
                                + itemIdStr + "\");'></td><td width='10%' algin='center style='display:none; '>"
                                + codeArr[i] + "</td><td width='25%' algin='center'>"
                                + nameArr[i] + "</td><td width='15%' algin='center'>"
                                + maxValueArr[i] + "</td><td width='15%' algin='center'>"
                                + minValueArr[i] + "</td><td width='15%' algin='center'>"
                                + standardValueArr[i] + "</td><td width='30%' algin='left'><select id='"
                                + itemIdStr + "'><option value='0'>未判定</option><option value='1'>正常</option><option value='2' selected='true'>异常</option></select></td></tr>"
                        } else {
                            strDel = "<tr ><td width='10%' algin='center'><input type='checkbox' onclick='setDelItem(\""
                                + itemIdStr + "\");'></td><td width='10%' algin='center style='display:none; '>"
                                + codeArr[i] + "</td><td width='25%' algin='center'>"
                                + nameArr[i] + "</td><td width='15%' algin='center'>"
                                + maxValueArr[i] + "</td><td width='15%' algin='center'>"
                                + minValueArr[i] + "</td><td width='15%' algin='center'>"
                                + standardValueArr[i] + "</td><td width='30%' algin='left'><select id='"
                                + itemIdStr + "'><option value='0' selected='true'>未判定</option><option value='1'>正常</option><option value='2'>异常</option></select></td></tr>"
                        }

                    }
                    if (idArr[i].split(":")[1] == "1") {
                        strDel = "<tr ><td width='10%' algin='center'><input type='checkbox' onclick='setDelItem(\""
                            + itemIdStr + "\");'></td><td width='10%' algin='center style='display:none; '>"
                            + codeArr[i] + "</td><td width='25%' algin='center'>"
                            + nameArr[i] + "</td><td width='15%' algin='center'>"
                            + maxValueArr[i] + "</td><td width='15%' algin='center'>"
                            + minValueArr[i] + "</td><td width='15%' algin='center'>"
                            + standardValueArr[i] + "</td><td width='30%' algin='center'><input id='"
                            + itemIdStr + "' type='text' value='"
                            + valueArr[i] + "'></td></tr>"
                    }

                    $("#chkItemTableHead").append(strDel);
                    //$api.html($api.byId("chkItemTableBody"), strDel);
                }
            }
            delItemIds = "";
        } else {
            alert("未选择要删除的点检项");
        }
    }


    function doSave(type) {
        console.log(itemIds)
        if (itemIds != "") {
            //点检项表格中有数据
            setItemValues();
            //提交数据到服务端
            doSubmit(type);
        } else {
            alert("无点检项信息，无法提交数据");
        }
    }

    function setItemValues() {
        josnList = [];
        var itemIdArr = itemIds.split(",");
        var itemValue = "";
        var un = localStorage.getItem('un');
        for (i = 0; i < itemIdArr.length; i++) {
            var itemStrArr = itemIdArr[i].split(":");
            var itemCodeArr = itemCodes.split(",");
            var itemNameArr = itemNames.split(",");
            var itemMaxValueArr = itemMaxValues.split(",");
            var itemMinValueArr = itemMinValues.split(",");
            var chkValue = "";
            var checkResult = "";
            var taskState = document.getElementById("zxState").value;
            var taskResult = document.getElementById("zxResult").value;
            var actualBeginDate = document.getElementById("actualBeginDate").value;
            var actualEndDate = document.getElementById("actualEndDate").value;
            if (itemStrArr[1] == "0") {
                //获取下拉框选中的值
                itemValue = document.getElementById(itemStrArr[0]).value;//选中的值
                checkResult = itemValue;
            }
            if (itemStrArr[1] == "1") {
                //获取文本框填的值
                itemValue = document.getElementById(itemStrArr[0]).value
                chkValue = itemValue;
                if (parseFloat(chkValue) >= parseFloat(itemMinValueArr[i]) && parseFloat(chkValue) <= parseFloat(itemMaxValueArr[i])) {
                    //检验结果 0未判定/1正常/2异常
                    checkResult = 1;
                } else {
                    checkResult = 2;
                }
            }

            if (i == 0) {
                itemValues = itemValue;
            } else {
                itemValues = itemValues + "," + itemValue;
            }
            //将点检项的结果转化为jsonList
            var chkBeginTime = document.getElementById("actualBeginDate").value;
            var iObj = new itemObj(itemStrArr[0], itemCodeArr[i], itemNameArr[i], chkValue, checkResult, un, taskState, taskResult, chkBeginTime);
            josnList.push(iObj);
        }
    }




    function itemObj(chkItemId, chkItemCode, chkItemName, chkValue, checkResult, chkPerson, taskState, taskResult, chkBeginTime) {
        this.chkItemId = chkItemId;
        this.chkItemCode = chkItemCode;
        this.chkItemName = chkItemName;
        this.chkValue = chkValue;
        this.checkResult = checkResult;
        this.taskState = taskState;
        this.taskResult = taskResult;
        this.chkBeginTime = chkBeginTime;
    }




    function doSubmit() {
        

        console.log(JSON.stringify(josnList))
        var type = document.getElementById("zxResult").value;
        var taskState = document.getElementById("zxState").value;
        var actualBeginDate = document.getElementById("actualBeginDate").value;
        var actualEndDate = document.getElementById("actualEndDate").value;
        api.showProgress();
        var equipCode = document.getElementById("sm").value;
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveTaskItem";
        var un = localStorage.getItem('un');

        var params = new Object();
        params.url = url;
        params.method = 'post';
        params.dataType = 'json';
        params.returnAll = 'false';
        params.timeout = 60;

        api.showProgress({
            title: '保存中...',
        });

        var data = {
            values: {
                un: un,
                // pw: pw,
                // url: url1,
                taskId: taskId,
                rowData: JSON.stringify(josnList),
                type: type,
                taskState: taskState,
                equipCode: equipCode,
                actualBeginDate: actualBeginDate,
                //actualEndDate: actualEndDate
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    //清空所有信息，重新扫设备选任务
                    taskId = "";
                    taskCode = "";
                    itemIds = "";
                    itemCodes = "";
                    itemNames = "";
                    itemModes = "";
                    itemValues = "";
                    josnList = [];

                    //清空设备扫码框				
                    $("#sm").val("").focus();
                    // 清空执行状态					
                    $("#zxState").val("2");
                    // 清空执行结果					
                    $("#zxResult").val("0");
                    // 清空执行时间
                    $("#actualBeginDate").attr("value", "");
                    // 清空执行时间
                    $("#actualEndDate").attr("value", "");
                    //清空任务选择
                    $("#chkTaskCode").attr("value", "");
                    //清空点检项表格
                    $("#chkItemTableHead").children().remove();
                } else if (myobj.code == '1') {

                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }

            } else {
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>